﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ReadWriteCsv;

namespace StockAnomalyDetector
{
    class BuyBackRecord : Record
    {
        public double SellPrice;
        public int SellQuantity;
        public double BuyPrice;
        public int BuyQuantity;

        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3},{4}", base.ToString(), SellPrice, SellQuantity, BuyPrice, BuyQuantity);
        }
    }

    /*
     * BuyBack：低位回购操作。大额抛出，而后散户跟抛，股价下降，再低位大额回购。
     */
    class BuyBack : Pattern
    {
        List<int> startTimes = new List<int>();
        List<int> endTimes = new List<int>();  
        List<double> sellPrices = new List<double>();
        List<double> buyPrices = new List<double>();
        List<int> sellQuantities = new List<int>();
        List<int> buyQuantities = new List<int>();

        private double total;
        private int count;
        private double target;
        private int startTime;
        private double sellPrice;
        private int sellQuantity;

        public BuyBack(CsvRow r) : base(r)
        {
        }

        public static List<string> exceptionList = new List<string>() { "stockcode,date,starttime,endtime,sellprice,sellquantity,buyprice,buyquantity" };//暂时存储所有Csv文件里的Repetition异常
        public static CsvFileWriter writer = new CsvFileWriter(@"D:\股票数据\" + "buyback" + ".csv");

        public override void Analyze()
        {
            ParseRow();

            total += price*quantity;
            count++;
            double avg = total/count; // 当前成交额的平均值
            
            if (price*quantity > avg*30 && BorS == 'S') // 如果大额抛出，更新检测目标
            {
                target = price*0.98;
                startTime = time;
                sellPrice = price;
                sellQuantity = quantity;
            }

            if (price < target) // 检测到距离大额抛出后，价格跌到一个区间内，在此区间找大额买入
            {
                if (price*quantity > avg*30 && BorS == 'B' && time-startTime<10000) // 发现大量买入，且相隔时间较短，记录异常
                {
                    startTimes.Add(startTime);
                    endTimes.Add(time);
                    sellPrices.Add(sellPrice);
                    buyPrices.Add(price);
                    sellQuantities.Add(sellQuantity);
                    buyQuantities.Add(quantity);
                    target = 0;
                    startTime = 0;
                }
            }
        }

        public override void End(string sourceName)
        {
            // 最后的处理工作，把所有异常数据扔进repetitionList，待外部UpdateExceptionRecord函数统一操作
            for (int i = 0; i < startTimes.Count; i++)
            {
                Record record = new BuyBackRecord()
                {
                    stockcode = Convert.ToString(Regex.Match(sourceName, @"[A-Z]{2}\d{6}")),
                    date = Convert.ToString(Regex.Match(sourceName, @"\d{8}")),
                    startTime = startTimes[i],
                    endTime = endTimes[i],
                    SellPrice = sellPrices[i],
                    BuyPrice = buyPrices[i],
                    SellQuantity = sellQuantities[i],
                    BuyQuantity = buyQuantities[i]
                };
                Console.WriteLine("BuyBack: "+record);
                exceptionList.Add(record.ToString());
            }
            
        }
    }
}
